//
//  KLAlert.swift
//  KLTips
//
//  Created by ni on 2024/6/14.
//

import Foundation

public class KLAlert {
    
    
    /// 系统级别弹窗
    /// - Parameters:
    ///   - title: 标题
    ///   - message: 内容
    ///   - placeholder: 指示文字
    ///   - okButtonTitle: 确认按钮
    ///   - cancelButtonTitle: 取消按钮文字
    ///   - textValue: 内容
    ///   - parent: 父视图
    ///   - completion: 回调
    public static func presentTextFieldAlert(title: String? = "title",
                                      message: String? = "message",
                                      placeholder: String = "placeholder",
                                      okButtonTitle: String = "OK",
                                      cancelButtonTitle: String = "Cancel",
                                      textValue:String = "",
                                      parent:UIViewController,
                                      completion: @escaping (String) -> Void) {
        let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alertVC.addTextField { textField in
            textField.placeholder = placeholder
            textField.font = UIFont.systemFont(ofSize: 16)
            textField.text = textValue
        }
        let ok = UIAlertAction(title: okButtonTitle, style: .default) { _ in
            if let name = alertVC.textFields?.first?.text, !name.isEmpty {
                completion(name)
            }
        }
        let cancel = UIAlertAction(title: cancelButtonTitle, style: .cancel)
        alertVC.addAction(cancel)
        alertVC.addAction(ok)
        parent.present(alertVC, animated: true)
    }
}
